<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- action：接口地址
    method：请求方法 -->
    <form action="http://ajax-api.itheima.net/api/data" method="post">
      <input name="username" type="text" placeholder="请输入用户名" />
      <br />
      <input name="food" type="text" placeholder="请输入喜欢的食物" />
      <br />
      <input name="sign" type="textarea" placeholder="请输入个性签名" />
      <br />
      <input name="pass" type="password" placeholder="请输入密码" />
      <br />
      <!-- type="submit"可以省略，因为默认值就是这一个 -->
      <button type="submit">提交</button>
    </form>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>
      // 1、阻止按钮的提交行为
      // document.querySelector('button').onclick = function (e) {
      //   // 阻止默认行为
      //   e.preventDefault()
      //   // 阻止冒泡
      //   // e.stopPropagation()
      // }
      // 2、阻止表单的提交行为
      document.querySelector('form').onsubmit = function (e) {
        e.preventDefault()
        axios({
          url: 'http://ajax-api.itheima.net/api/data',
          method: 'post',
          data: {
            username: document.querySelector('[name=username]').value,
            food: document.querySelector('[name=food]').value,
            sign: document.querySelector('[name=sign]').value,
            pass: document.querySelector('[name=pass]').value,
          },
        }).then((res) => {
          console.log(res)
        })
      }
    </script>
  </body>
</html>
